#ifndef __MOTOR_H
#define __MOTOR_H
/* Include */
#include "stm32g4xx.h"
#include "svpwm.h"
#include "tim.h"
#include "adc.h"
#include "opamp.h"
#include "tim.h"
#include "my_math.h"
#include "state_machine.h"
#include "ADC_Calc.h"
#include "CurrentLoop.h"
#include "SpeedLoop.h"
#include "Encoder.h"
#include "PI_Regulator.h"
#include "task.h"
#include "flux.h"
#include "math.h"
#include "Wind_detection.h"
#include "usart.h"
#include "wave.h"
#include "VOFA.h"
#include "string.h"
#include "stdio.h"
#include "ADRC.h"
#include "test.h"
#include "key.h"
#include "stdbool.h"
#include "Kalman.h" 
/*System Configure */
#define System_Clock  160000000
#define PWM_FRE       20000
#define PWM_PERIOD 		(System_Clock/PWM_FRE/2)
#define Vdc           24
#define SpeedLoop_FRE     1000
#define LOOPTIME_SEC     	0.00005f

#define SpeedLOOP_SEC			0.001f

#define IRP_PERCALC   PWM_FRE/SpeedLoop_FRE

/*MOTOR Parameter*/
#define SpeedMAX       3300  //RPM
#define Ib              4
#define Vb             Vdc/1.732f

#define PR_PULSE       10000
#define Polar          5

#define Ld_realvalue 	 0.0001075f
#define Lq_realvalue 	 0.0001261f
#define Ls_realvalue   0.0001168f
#define Lam_realvalue  0.0005f
#define Rs_realvalue 	 0.475f
#define fn             SpeedMAX/60*Polar

#define Rb						 Vb/Ib
#define wb             6.28f*fn
#define Tb						 1/wb 
#define Lb							Rb*Tb

/*Curr_ADC_Gain*/
#define Cur_AMP_Gain     				 7.33f              
#define Curr_Sampling_Res        0.005f              
#define Curr_ADC_Gain            (16384/(Ib*Curr_Sampling_Res*Cur_AMP_Gain/3.3f)/4095) 

/*Volt_ADC_Gain */
#define Volt_Res_Rate            3/(3+75)
#define Volt_ADC_Gain            (int16_t)((8192*1024)/(Vb*Volt_Res_Rate/3.3f)/4095)


/*      EN         */
#define BUS_Detect_En 		  0 
#define SAMPLE_CURR_LPF_EN  0
#define OVER_MOD_EN         1

/**
  * @brief if step parameters
  */
typedef struct
{
    int16_t desired_vel;
    float step;
    int16_t iq;
    int16_t id;
} if_t;
extern if_t if_handler;
int16_t IF_Start(int16_t desired_speed, float step);
/*      function         */
void FOC_ExecuteAndUpdatePWM(void);
void Motor_Init(void);
#endif
